package com.tuosha.controller;

import java.util.List;
import java.util.Map;

import com.irs.annotation.SysLog;
import com.irs.util.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import com.tuosha.constant.Constants;
import com.tuosha.util.Query;
import com.tuosha.base.controller.BaseController;
import com.tuosha.model.XinYongKasEntity;
import com.tuosha.service.XinYongKasService;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 
 * @author tuosha
 * @email 1044053532@qq.com
 * @date 2018-07-25 09:38:57
 */
@Controller
@RequestMapping("xinyongka")
public class XinYongKasController extends BaseController{
	@Autowired
	private XinYongKasService xinYongKasServiceImpl;
	
	/**
	 * 页面
	 */
	@RequestMapping("/page")
	public String page(@RequestParam Map<String, Object> params){
		return "page/xinyongka/xinyongkas";
	}
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(@RequestParam Map<String, Object> params){
	    Query query = new Query(params);
		List<XinYongKasEntity> xinYongKasList = xinYongKasServiceImpl.queryList(query);
		int total = xinYongKasServiceImpl.queryTotal(query);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",total,xinYongKasList);
	}

    /**
     * 增加页面
     */
    @RequestMapping("addXYK")
    @RequiresPermissions("xinyongka:xinyongka:save")
    public String cardAdd(){
        return "page/xinyongka/addXYK";
    }

    @RequestMapping("/checkXYKName/{name}")
    @ResponseBody
    public ResultUtil checkXYKName(@PathVariable("name")String name) {
        XinYongKasEntity xinyongka = xinYongKasServiceImpl.queryName(name);
        if(xinyongka!=null){
            return new ResultUtil(500,"信用卡名称已存在！");
        }
        return new ResultUtil(0);
    }

	
	/**
	 * 保存
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(@ModelAttribute XinYongKasEntity xinYongKas){
		xinYongKasServiceImpl.save(xinYongKas);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"提交成功！",0,xinYongKas);
	}

	/**
	 * 修改待信息显示
	 */

	@RequestMapping(value="/editXYK/{id}")
	@RequiresPermissions("xinyongka:xinyongka:update")
	public String editXYK(HttpServletRequest req, @PathVariable("id")Integer id) {
		XinYongKasEntity xinYongKas = xinYongKasServiceImpl.queryObject(id);

		req.setAttribute("xinyongka",xinYongKas);

		return "page/xinyongka/editXYK";
	}
	/**
	 * 修改
	 */
	@RequestMapping(value="/update", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@ModelAttribute XinYongKasEntity xinYongKas){
		int result = xinYongKasServiceImpl.update(xinYongKas);
		return putMsgToJsonString(result,"",0,"");
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(@RequestParam Integer[] ids){
		int result = xinYongKasServiceImpl.deleteBatch(ids);
		return putMsgToJsonString(result,"",0,"");
	}

	@SysLog(value="删除信用卡信息")
	@RequestMapping("/delById/{id}")
	@RequiresPermissions("xinyongka:xinyongka:delete")
	@ResponseBody
	public ResultUtil delById(@PathVariable("id")Integer id) {

		try {
			xinYongKasServiceImpl.delById(id);
			return ResultUtil.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error();
		}
	}

	/**
	 * 批量删除指定管理员
	 * @param //id
	 * @return
	 */
	@SysLog(value="批量删除信用卡信息")
	@RequestMapping("/delPatch/{Str}")
	@RequiresPermissions("xinyongka:xinyongka:delete")
	@ResponseBody
	public ResultUtil delPatch(@PathVariable("Str")String xykStr) {
//		String[] strs = xykStr.split(",");
//		for (int i=0;i<strs.length;i++) {
//
//			try {
//				int a= xinYongKasServiceImpl.delById(Integer.parseInt(strs[i]));
//				//System.out.println("jieguo:"+a+":jiluID:"+Integer.parseInt(strs[i]));
//			} catch (Exception e) {
//				e.printStackTrace();
//				return ResultUtil.error();
//			}
//		}
//		return ResultUtil.ok();
		try {
			xinYongKasServiceImpl.delPatch(xykStr);
			return ResultUtil.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error();
		}

	}

}
